ostree-prepare-root: Refactor code to resolve deploy_path
authorWilliam Manley <will@williammanley.net>
Mon, 18 Jul 2016 13:42:40 +0000 (14:42 +0100)
committerAtomic Bot <atomic-devel@projectatomic.io>
Tue, 2 Aug 2016 19:07:25 +0000 (19:07 +0000)
I'll reuse this for a new ostree-init.

Closes: #403
Approved by: cgwalters

src/switchroot/ostree-prepare-root.c

index 375867b1eabfefb066c10c52fc2a63d4515aa312..df3e32198fc260c0b98bdfdc6db4ab8bc65775fc 100644 (file)
@@ -108,25 +108,12 @@ touch_run_ostree (void)
   (void) close (fd);
 }
 
-int
-main(int argc, char *argv[])
+static char*
+resolve_deploy_path (const char * root_mountpoint)
 {
-  const char *root_mountpoint = NULL;
-  char *ostree_target = NULL;
-  char *deploy_path = NULL;
-  char srcpath[PATH_MAX];
   char destpath[PATH_MAX];
-  char newroot[PATH_MAX];
   struct stat stbuf;
-  int orig_cwd_dfd;
-
-  if (argc < 2)
-    {
-      fprintf (stderr, "usage: ostree-prepare-root SYSROOT\n");
-      exit (EXIT_FAILURE);
-    }
-
-  root_mountpoint = argv[1];
+  char *ostree_target, *deploy_path;
 
   ostree_target = parse_ostree_cmdline ();
   if (!ostree_target)
@@ -135,16 +122,6 @@ main(int argc, char *argv[])
       exit (EXIT_FAILURE);
     }
 
-  /* Create a temporary target for our mounts in the initramfs; this will
-   * be moved to the new system root below.
-   */
-  snprintf (newroot, sizeof(newroot), "%s.tmp", root_mountpoint);
-  if (mkdir (newroot, 0755) < 0)
-    {
-      perrorv ("Couldn't create temporary sysroot '%s': ", newroot);
-      exit (EXIT_FAILURE);
-    }
-
   snprintf (destpath, sizeof(destpath), "%s/%s", root_mountpoint, ostree_target);
   printf ("Examining %s\n", destpath);
   if (lstat (destpath, &stbuf) < 0)
@@ -164,6 +141,38 @@ main(int argc, char *argv[])
       exit (EXIT_FAILURE);
     }
   printf ("Resolved OSTree target to: %s\n", deploy_path);
+  return deploy_path;
+}
+
+int
+main(int argc, char *argv[])
+{
+  const char *root_mountpoint = NULL;
+  char *deploy_path = NULL;
+  char srcpath[PATH_MAX];
+  char destpath[PATH_MAX];
+  char newroot[PATH_MAX];
+  struct stat stbuf;
+  int orig_cwd_dfd;
+
+  if (argc < 2)
+    {
+      fprintf (stderr, "usage: ostree-prepare-root SYSROOT\n");
+      exit (EXIT_FAILURE);
+    }
+
+  root_mountpoint = argv[1];
+  deploy_path = resolve_deploy_path (root_mountpoint);
+
+  /* Create a temporary target for our mounts in the initramfs; this will
+   * be moved to the new system root below.
+   */
+  snprintf (newroot, sizeof(newroot), "%s.tmp", root_mountpoint);
+  if (mkdir (newroot, 0755) < 0)
+    {
+      perrorv ("Couldn't create temporary sysroot '%s': ", newroot);
+      exit (EXIT_FAILURE);
+    }
   
   /* Work-around for a kernel bug: for some reason the kernel
    * refuses switching root if any file systems are mounted